/**
 * fileName:  ParamValueRelReader.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-11 下午11:25:57
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-11          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.bean.ChooseType;
import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.ParamValueRelEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.CategoryArgumentRelationEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.CategoryArgumentRelationMapper;
import org.springframework.stereotype.Service;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  ParamValueRelReader
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Service
public class ParamValueRelReader implements DatabaseReader<ParamValueRelEntity> {
	
	@Resource(name = "categoryArgumentRelationMapper")
	private CategoryArgumentRelationMapper relationMapper;

	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public ParamValueRelReader() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * <pre>
	 * 根据引用编号查询引用关系
	 * </pre>
	 *
	 * @param relationId
	 * @return   
	 */
	public ParamValueRelEntity findParamValueRelById(String relationId) {
		
		CategoryArgumentRelationEntity entityDao = relationMapper.getById(relationId);
		
		ChooseType type = null;
		if (ChooseType.RADIO.value() == entityDao.getChooseType()) {
			type = ChooseType.RADIO;
		} else if (ChooseType.MULIT.value() == entityDao.getChooseType()) {
			type = ChooseType.MULIT;
		} else {
			type = ChooseType.CUSTOM;
		}
		
		return new ParamValueRelEntity(entityDao.getId(), entityDao.getFruitCategoryId(), entityDao.getFruitArgumentId(), entityDao.getArgumentCostId(), type, (int) entityDao.getSort());
		
	}
	
	/**
	 * <pre>
	 * 按分类编号查询引用参数信息
	 * </pre>
	 *
	 * @param categoryId
	 * @return   
	 */
	public List<ParamValueRelEntity> findParamValueRelByCategoryId(String categoryId) {
		
		List<CategoryArgumentRelationEntity>  reslutDao = relationMapper.getRelationByCategoryId(categoryId);
		List<ParamValueRelEntity> result = new ArrayList<ParamValueRelEntity>(reslutDao.size());
		ChooseType type = null;
		for (CategoryArgumentRelationEntity entityDao : reslutDao) {
			if (ChooseType.RADIO.value() == entityDao.getChooseType()) {
				type = ChooseType.RADIO;
			} else if (ChooseType.MULIT.value() == entityDao.getChooseType()) {
				type = ChooseType.MULIT;
			} else {
				type = ChooseType.CUSTOM;
			}
			
			result.add(new ParamValueRelEntity(entityDao.getId(), entityDao.getFruitCategoryId(), entityDao.getFruitArgumentId(), entityDao.getArgumentCostId(), type, (int) entityDao.getSort()));
		}
		
		return result;
	}

}
